package com.example.ggxiaozhi.store.the_basket.utils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.os.Environment;
import android.util.Log;

public class Logger {
    public static final String FORMAT_DATE = "yyyy-MM-dd";
    public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    private static StringBuffer LOG_BUFFER = new StringBuffer();
    public static String LOG_DIR = "logs";
    private static int LOG_MAX_BUFFER = 2048;
    public static boolean LOG_ON_D = false;
    public static boolean LOG_ON_E = false;
    public static boolean LOG_ON_I = false;
    public static boolean LOG_ON_W = false;
    public static boolean TO_FILE = false;

    private static void cleanLogFile(File file) {
        String[] dateAndPrev = getDateAndPrev(1);
        if (dateAndPrev != null && dateAndPrev.length > 0) {
            File[] listFiles = file.listFiles();
            int length = listFiles != null ? listFiles.length : 0;
            for (int i = 0; i < length; i++) {
                int i2;
                File file2 = listFiles[i];
                String name = file2.getName();
                for (String startsWith : dateAndPrev) {
                    if (name.startsWith(startsWith) && name.endsWith("log")) {
                        i2 = 0;
                        break;
                    }
                }
                i2 = 1;
                if (i2 != 0) {
                    System.out.println("Logger.cleanLogFile() === delete ..." + file2.delete());
                }
            }
        }
    }

    public static void clear() {
        new Thread() {
            public void run() {
                Logger.writeLog2File(true);
            }
        }.start();
    }

    public static float countTime(long j) {
        return ((float) (System.currentTimeMillis() - j)) / 1000.0f;
    }

    public static void d(String str, String str2) {
        if (str2 != null) {
            if (TO_FILE) {
                log2Buffer("D", str, str2);
            }
            if (LOG_ON_D) {
                Log.d(str, str2);
            }
        }
    }

    @SuppressLint("SimpleDateFormat")
	public static String dateToString(Date date, String str) {
        String str2 = null;
        if (date != null) {
            try {
                str2 = new SimpleDateFormat(str).format(date);
            } catch (Exception e) {
            }
        }
        return str2;
    }

    public static void e(String str, String str2) {
        if (str2 != null) {
            if (TO_FILE) {
                log2Buffer("E", str, str2);
            }
            if (LOG_ON_E) {
                Log.e(str, str2);
            }
        }
    }

    public static String[] getDateAndPrev(int i) {
        int i2 = 0;
        Calendar instance = Calendar.getInstance();
        if (i <= 0) {
            return new String[]{dateToString(new Date(), "yyyy-MM-dd")};
        }
        String[] strArr = new String[(i + 1)];
        strArr[0] = dateToString(new Date(), "yyyy-MM-dd");
        while (i2 < i) {
            instance.roll(6, -1);
            strArr[i2 + 1] = dateToString(instance.getTime(), "yyyy-MM-dd");
            i2++;
        }
        return strArr;
    }

    public static void i(String str, String str2) {
        if (str2 != null) {
            if (TO_FILE) {
                log2Buffer("I", str, str2);
            }
            if (LOG_ON_I) {
                Log.i(str, str2);
            }
        }
    }

    private static void log2Buffer(String str, String str2, String str3) {
        StringBuffer stringBuffer = new StringBuffer();
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        if (stackTrace != null && stackTrace.length >= 3) {
            stringBuffer.append(str).append("|").append(dateToString(new Date(), "yyyy-MM-dd HH:mm:ss")).append("|").append(stackTrace[2].getClassName()).append("|").append(stackTrace[2].getMethodName()).append("|").append(stackTrace[2].getLineNumber());
            LOG_BUFFER.append(stringBuffer.toString()).append("|").append(str3).append("\n");
            if (LOG_BUFFER.length() >= LOG_MAX_BUFFER) {
                new Thread() {
                    public void run() {
                        Logger.writeLog2File(false);
                    }
                }.start();
            }
        }
    }
    
    public static void setLogState(boolean z) {
    	setLogState(z,LOG_MAX_BUFFER);
    }
    public static void setLogState(boolean z,int buffersize) {
        LOG_ON_E = z;
        LOG_ON_I = z;
        LOG_ON_W = z;
        LOG_ON_D = z;
        TO_FILE = z;
        if (new File(Environment.getExternalStorageDirectory(), "wb.l").exists()) {
            LOG_ON_E = true;
            LOG_ON_I = true;
            LOG_ON_W = true;
            TO_FILE = true;
            LOG_ON_D = true;
        }
        LOG_MAX_BUFFER = buffersize;
    }

    public static void w(String str, String str2) {
        if (str2 != null) {
            if (TO_FILE) {
                log2Buffer("W", str, str2);
            }
            if (LOG_ON_W) {
                Log.w(str, str2);
            }
        }
    }
    
    private static void writeLog2File(boolean r7) {
     	if(r7 || LOG_BUFFER.length() >= LOG_MAX_BUFFER ||  Environment.MEDIA_MOUNTED .equals(Environment.getExternalStorageState())){
    		String logfilename = dateToString(new Date(), "yyyy-MM-dd")+".log";
            File r3 =   new File(android.os.Environment.getExternalStorageDirectory(), LOG_DIR);
            if(!r3.exists()) r3.mkdirs();
            File  r4 = new  File(r3,logfilename);
            if(!r4.exists())
				try {
					r4.createNewFile();
				} catch (IOException e) {
					return ;
 				}
            
            java.io.FileWriter fw = null;
			try {
				fw = new java.io.FileWriter(r4,true);
			  	fw.write(LOG_BUFFER.toString());
				fw.flush();
	    		LOG_BUFFER.delete(0, LOG_BUFFER.length());

			} catch (IOException e) {
				cleanLogFile(r3);
			} finally {
		  
				if(fw != null)
					try {
						fw.close();
					} catch (IOException e1) {
					 
					}
 			}	 
            	  
            
     	}
    }
}
